package com.xianyu.admin.utils;

import com.google.common.base.CaseFormat;

public class StringUtils {
    public static String typeMapping(String dbType) {
        String javaType;
        if ("int|integer".contains(dbType)) {
            javaType = "Integer";
        } else if ("float|double|decimal|real".contains(dbType)) {
            javaType = "Double";
        } else if ("date|time|datetime|timestamp".contains(dbType)) {
            javaType = "Date";
        } else {
            javaType = "String";
        }
        return javaType;
    }

    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder(1024);
        if (underscoreName != null && !underscoreName.isEmpty()) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    public static String tableNameConvertLowerCamel(String tableName) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
    }

    public static String tableNameConvertUpperCamel(String tableName) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());

    }

    public static String tableNameConvertMappingPath(String tableName) {
        tableName = tableName.toLowerCase();//兼容使用大写的表名
        return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
    }

    public static String modelNameConvertMappingPath(String modelName) {
        String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
        return tableNameConvertMappingPath(tableName);
    }

    public static String packageConvertPath(String packageName) {
        return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
    }

}
